// src/models/user.ts
import { getUserInfo } from '@/services/userApi';

export interface UserInfo {
  id: number;
  userName: string;
  nickName: string;
  role: string;
  permissions: string[];
  // ...其他字段
}

export default {
  state: {
    currentUser: {} as UserInfo,
    loading: true
  },
  reducers: {
    saveUser(state: any, payload: UserInfo) {
      return { ...state, currentUser: payload, loading: false };
    },
    clearUser(state: any) {
      return { ...state, currentUser: {}, loading: false };
    }
  },
  effects: {
    *fetchUser(_: void, { call, put }: any) {
      try {
        const response = yield call(getUserInfo);
        yield put({ type: 'saveUser', payload: response.data });
      } catch (error) {
        yield put({ type: 'clearUser' });
      }
    }
  }
};